package cn.glutnn.controller;


import cn.glutnn.entity.Discuss;
import cn.glutnn.entity.Knowledge;
import cn.glutnn.entity.Result;
import cn.glutnn.entity.dto.DiscussesDTO;
import cn.glutnn.service.DiscussService;
import cn.glutnn.service.KnowledgeService;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;


/**
 * (Knowledge)表控制层
 *
 * @author msh
 * @since 2024-10-25 09:34:51
 */
@Tag(name = "知识管理模块")
@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {
    /**
     * 服务对象
     */
    @Autowired
    private KnowledgeService knowledgeService;
    // 分页条件查询所有知识
    @Operation(summary = "分页条件查询所有知识")
    @GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(defaultValue = "") String key) {
        if (key.isEmpty()) return Result.success(knowledgeService.lambdaQuery().page(new Page<>(pageNum, pageSize)));
        return Result.success(knowledgeService.lambdaQuery().like(Knowledge::getTitle, key).page(new Page<>(pageNum, pageSize)));
    }

    // 根据id查询知识
    @Operation(summary = "根据id查询知识")
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        try {
            return Result.success(knowledgeService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    // 根据登录用户查询知识
    @Operation(summary = "根据登录用户查询知识")
    @GetMapping("/selectByUsername")
    public Result selectByUsername() {
        Map map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        return Result.success(knowledgeService.lambdaQuery().eq(Knowledge::getOwnName, username).list());
    }

    // 添加知识
    @Operation(summary = "添加知识")
    @PostMapping
    public Result add(@RequestBody Knowledge knowledge) {
        Map map = ThreadLocalUtil.get();
        knowledge.setOwnName((String) map.get("username"));
        knowledge.setCreateTime(LocalTime.now());
        knowledge.setUpdateTime(new Date(System.currentTimeMillis()));
        try {
            knowledgeService.save(knowledge);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
        return Result.success();
    }

    // 根据id修改知识
    @Operation(summary = "根据id修改知识")
    @PutMapping("/{id}")
    public Result updateById(@PathVariable("id") Integer id, @RequestBody Knowledge knowledge) {
        knowledge.setKnowledgeId(id);
        try {
            knowledgeService.lambdaUpdate().eq(Knowledge::getKnowledgeId, id).update(knowledge);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败");
        }
        return Result.success();
    }

    // 根据id删除知识
    @Operation(summary = "根据id删除知识")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            knowledgeService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
        return Result.success();
    }


}

